package zhai.docking.alipay.service.base.bizlog;


import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import zhai.docking.alipay.constant.LogConstant;
import zhai.docking.alipay.domain.base.bizlog.BizLogData;
import zhai.docking.alipay.domain.base.bizlog.BizLogGet;
import zhai.docking.alipay.domain.base.bizlog.BizLogResult;
import zhai.docking.alipay.domain.base.bizlog.BizLogSave;
import zhai.docking.alipay.sync.base.bizlog.BizLogAsync;
import zhai.docking.alipay.util.JSONUtil;
import zhai.docking.alipay.util.StringUtil;

/**
 * @author Created by 翟凤玺
 * @package zhai.docking.alipay.service.base.bizlog
 * @project-name zhai-docking-alipay
 * @description 业务日志持久化 实现
 * @create-datetime 2020-1-19 11:52
 */
@Slf4j
@Service
public class BizLogSerImpl implements BizLogSer {

    private static final String BUSINESS_NAME = "【业务日志持久化 服务实现】";
    private static final String METHOD_SAVE = "（保存）";
    private static final String METHOD_GET = "（获取）";

    @Autowired
    BizLogAsync async;
    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public BizLogResult save(BizLogSave in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_SAVE, LogConstant.IN, JSONUtil.toStr(in)));
        /** mongodb */
        if (BizLogSave.DB_TYPE_MONGODB.equals(in.getDbType())) {
            /** 异步 */
            if (BizLogSave.SAVE_TYPE_ASYNC.equals(in.getSaveType())) {
                async.mongodb(in);
            }
        } else {
            log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_SAVE, LogConstant.RESULT, LogConstant.FAIL));
            return BizLogResult.failture();
        }
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_SAVE, LogConstant.RESULT, LogConstant.SUCCESS));
        return BizLogResult.success();
    }

    @Override
    public BizLogResult get(BizLogGet in) {
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_GET, LogConstant.IN, JSONUtil.toStr(in)));
        BizLogData data = mongoTemplate.findById(in.getId(), BizLogData.class, in.getDbName());
        log.info(StringUtil.join(BUSINESS_NAME, LogConstant.SERVICE, METHOD_GET, LogConstant.RESULT, LogConstant.SUCCESS));
        return BizLogResult.success(data);
    }
}
